

import { Menu, Tray, BrowserWindow, app,nativeImage } from 'electron'
import icon from '../renderer/src/assets/images/logo.png'
import { createWindow as createMainWindow } from './mainWindow'

export function createTray() : Tray{
  const path=nativeImage.createFromDataURL(icon)
  const iconTray = new Tray(path)

// 配置右键菜单
  const trayMenu = Menu.buildFromTemplate([
    {
      label: '设置',
      click: function () {
        openWindows()
      }
    },
    {
      label: '退出Autumn',
      click: function () {
        if (process.platform !== 'darwin') {
          app.quit()
        }
      }
    }
  ])
  // 鼠标悬停托盘提示
  iconTray.setToolTip('Autumn')
// 绑定右键菜单到托盘
  iconTray.setContextMenu(trayMenu)
// 任务栏图标双击托盘打开应用
  iconTray.on('click', function () {
    openWindows()

  })

  const openWindows=()=>{
    const allWindows = BrowserWindow.getAllWindows();
    if(allWindows.length>0){
      allWindows.forEach((window, index) => {
        window.show()
      });
    }else {
      createMainWindow()

    }

  }


  return iconTray;
}


